package com.medicalservice.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medicalservice.entity.Department;
import com.medicalservice.enums.ResultCodeEnum;
import com.medicalservice.exception.CustomException;
import com.medicalservice.mapper.DepartmentMapper;
import com.medicalservice.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Department department) {
        //检查科室是否已经存在
        Department existDepartment = departmentMapper.selectByName(department.getName());
        if (ObjectUtil.isNotNull(existDepartment)){
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }

        //设置默认值
        if (ObjectUtil.isNull(department.getStatus())){
            department.setStatus(1);         //1正常  0停用
        }
        if (ObjectUtil.isNull(department.getSortOrder())){
            department.setSortOrder(0);
        }
        if(ObjectUtil.isNull(department.getRegistrationFee())){
            department.setRegistrationFee(30);        //挂号费
        }

        department.setCreateTime(LocalDateTime.now());
        department.setUpdateTime(LocalDateTime.now());
        departmentMapper.insert(department);

    }


    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Integer id){
        Department department = departmentMapper.selectDepartmentById(id);
        if (ObjectUtil.isNull(department)) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }

        //查看是否有医生关联此科室.如果还有医生关联科室，就不能删除科室
        if (ObjectUtil.isNotNull(department.getDoctorCount()) && department.getDoctorCount() > 0){
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        departmentMapper.deleteById(id);
    }


    //修改科室信息
    public void updateById(Department department) {
        department.setUpdateTime(LocalDateTime.now());
        departmentMapper.updateById(department);
    }


    //根据ID查询科室

    public Department selectById(Integer id) {

        return departmentMapper.selectDepartmentById(id);
    }


    //查询所有科室
    public List<Department> selectAll(Department department) {

        return departmentMapper.selectAll(department);
    }


    //分页查询科室
    public PageInfo<Department> selectPage(Department department,Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Department> list = departmentMapper.selectAll(department);
        return PageInfo.of(list);
    }

    //按照状态统计科室数量
    public Integer countByStatus(Integer status){
        return departmentMapper.countByStatus(status);
    }
}



